package com.cn.wanxi.servlet;

import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/NewsSelect")
public class NewsSelect extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.获取页面传递过来的值
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
//        2.封装model
        NewsModel newsModel = new NewsModel();
        newsModel.setTitle(title);
        newsModel.setContent(content);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date ss = simpleDateFormat.parse(startTime);
            newsModel.setStartTime(ss);

            Date es = simpleDateFormat.parse(endTime);
            newsModel.setEndTime(es);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        newsModel.setStartT(startTime);
        newsModel.setEndT(endTime);

//        3.调用逻辑层
        NewsService newsService = new NewsService();
        List<NewsModel> list = newsService.findNews(newsModel);
//        4.返回页面
        req.setAttribute("list", list);

        req.setAttribute("newsModel", newsModel);
        req.getRequestDispatcher("/jsp/news.jsp").forward(req, resp);


    }
}
